package com.wanmait.community.config;

import com.wanmait.community.interceptor.JWTInterceptor;
import com.wanmait.community.interceptor.JWTUserInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import javax.annotation.Resource;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    //前后端分离访问静态资源
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {//访问静态资源图片的配置
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }
    //配置前后端分离
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")//当前跨域的配置针对那个路径
                .allowedOrigins("*")//允许那些域
                .allowedMethods("*")//那种请求方式
                .allowedHeaders("*")//那种请求头
                //.allowCredentials(true); 是否允许发送凭证，如果为true，前端axios也需要配合使用相应的设置，才能使用cookie，才能使用session
                .maxAge(3600);//预检请求的缓存时间
    }
    //配置用户登录拦截器
    @Resource
    private JWTUserInterceptor jwtUserInterceptor;
    //配置物业管理员的拦截器
    @Resource
    private JWTInterceptor jwtInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(jwtUserInterceptor)
                .addPathPatterns("/user/**")
                .excludePathPatterns("/user/login");
        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns("/admin/**")
                .excludePathPatterns("/login");

    }


}
